#include "graphics/d3d/ood3ddisplaywindow.h"

//------------------------------------------------------------------------
ooD3DDisplayWindow::ooD3DDisplayWindow()
:m_width(800)
,m_height(600)
,m_handle(DBBNULL)
{

}

//------------------------------------------------------------------------
void
ooD3DDisplayWindow::openWindow()
{
	WNDCLASSA    wndClass;      

	wndClass.style          = 0;
	wndClass.lpfnWndProc    = WndProc;
	wndClass.cbClsExtra     = 0;
	wndClass.cbWndExtra     = 0;
	wndClass.hInstance      = GetModuleHandle(0);
	wndClass.hIcon          = LoadIcon(0, IDI_WINLOGO);
	wndClass.hCursor        = LoadCursor(0, IDC_ARROW);
	wndClass.hbrBackground  = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
	wndClass.lpszMenuName   = 0;
	wndClass.lpszClassName  = m_name.cString();

	HWND window = 0;

	// register class.  Report error & exit upon failure
	if (RegisterClassA(&wndClass))
	{
		// get size of window to use so that client area is desired size
		RECT wnd_size = {0,0,m_width,m_height};
		AdjustWindowRect(&wnd_size, WS_MINIMIZEBOX|WS_CAPTION|WS_SYSMENU, FALSE);

		// create new window
		window = CreateWindowA(m_name.cString(), m_name.cString(),
			WS_MINIMIZEBOX|WS_CAPTION|WS_SYSMENU,
			0, 0,
			wnd_size.right - wnd_size.left,
			wnd_size.bottom - wnd_size.top,
			0, 0, GetModuleHandle(0), 0);
	}
	else
	{
		dbbassert(false);
	}
	m_handle = window;
	ShowWindow(m_handle, SW_NORMAL);
	UpdateWindow(m_handle);
}

//------------------------------------------------------------------------
void
ooD3DDisplayWindow::closeWindow()
{
	PostQuitMessage(0);
}

//------------------------------------------------------------------------
void*
ooD3DDisplayWindow::getWindowHandle() const
{
	return m_handle;
}

//------------------------------------------------------------------------
void 
ooD3DDisplayWindow::setName( const String& name )
{
	m_name		= name;
}

//------------------------------------------------------------------------
const String&
ooD3DDisplayWindow::getName() const
{
	return m_name;
}

//------------------------------------------------------------------------
void
ooD3DDisplayWindow::setSize(int width,int height)
{
	dbbassert(width>0);
	dbbassert(height>0);
	m_width		= width;
	m_height	= height;
}

//------------------------------------------------------------------------
int
ooD3DDisplayWindow::getWidth() const
{
	return m_width;
}

//------------------------------------------------------------------------
int
ooD3DDisplayWindow::getHeight() const
{
	return m_height;
}

//------------------------------------------------------------------------
void 
ooD3DDisplayWindow::tick(float elapsed)
{
	MSG msg;
	if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

//------------------------------------------------------------------------
LRESULT CALLBACK
ooD3DDisplayWindow::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}
